package com.android.model.db;

import android.content.Context;
import android.graphics.Color;

import com.android.model.entity.eventbus.BusUtils;
import com.android.model.info.LocalSp;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

/**
 * 颜色转换器
 * Created by TenFace on 2017/12/27.
 */

public class TransformColorUtil {

    public static Context mContext;
    public static volatile TransformColorUtil instance;

    private int beginColor = Color.argb(255, 255, 46, 91);//红
    private int endColor = Color.argb(255, 52, 224, 144);//绿
    private int defaultColor = Color.argb(255, 110, 109, 115);//灰

    public TransformColorUtil(Context context) {
        mContext = context;
//        EventBus.getDefault().register(mContext);
    }

    public static TransformColorUtil getInstance(Context context) {
        mContext = context;
        if (instance == null) {
            synchronized (TransformColorUtil.class) {
                if (instance == null) {
                    instance = new TransformColorUtil(context);
                }
            }
        }
        return instance;
    }

    public void colorStart() {

        if (LocalSp.getInstance(mContext).getBeginColor() != 0) {
            this.beginColor = LocalSp.getInstance(mContext).getBeginColor();
        }

        if (LocalSp.getInstance(mContext).getEndColor() != 0) {
            this.endColor = LocalSp.getInstance(mContext).getEndColor();
        }

    }

    @Subscribe
    public void onEventMainThread(BusUtils.TransformColor transformColor) {
        beginColor = transformColor.getBeginColor();
        endColor = transformColor.getEndColor();
        LocalSp.getInstance(mContext).saveBeginColor(beginColor);
        LocalSp.getInstance(mContext).saveEndColor(endColor);
    }

    public void setBeginColor(int beginColor) {
        this.beginColor = beginColor;
    }

    public void setEndColor(int endColor) {
        this.endColor = endColor;
    }

    public void setDefaultColor(int defaultColor) {
        this.defaultColor = defaultColor;
    }

    public int getBeginColor() {
        return beginColor;
    }

    public int getEndColor() {
        return endColor;
    }

    public int getDefaultColor() {
        return defaultColor;
    }

    public void colorEnd() {
        EventBus.getDefault().unregister(mContext);
    }

}
